//
// Created by xili on 2024/8/11 13:52.
// Go big or go home.
//
#include "leetcode.h"

class Solution {
public:
    string reverseMessage(string message) {
        int n = message.size();
        string ans;
        for (int i = n - 1; i >= 0; i--) {
            if (message[i] != ' ') {
                int j = i;
                while (j >= 0 && message[j] != ' ') {
                    j--;
                }
                ans += message.substr(j + 1, i - j + 1) + ' ';
                i = j - 1;
            }
        }
        return ans;
    }
};